package com.qianya.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianya.oa.common.core.controller.BaseController;
import com.qianya.oa.common.core.domain.entity.SysUser;
import com.qianya.oa.common.utils.poi.ExcelUtil;
import com.qianya.oa.domain.UserSign;
import com.qianya.oa.service.UserSignService;
import com.qianya.oa.util.CommonResult;
import com.qianya.oa.vo.UserSignVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/UserSign")
public class UserSignController extends BaseController {

    @Resource
    private UserSignService userSignService;

    @GetMapping("/getAllSigns")
    @RequiresRoles(value = {"admin","boss","hr"},logical = Logical.OR)
    public CommonResult<?> getAllSigns(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<UserSignVo> pageInfo = new PageInfo<>(userSignService.getAllUserSigns());
        return CommonResult.success(pageInfo);
    }

    @GetMapping("/getSignByUserId")
    public CommonResult<?> getSignByUserId(){
        Long userId = getSysUser().getUserId();
        return CommonResult.success(userSignService.getUserSignsByUserId(Math.toIntExact(userId)));
    }

    @GetMapping("/getSignByid")
    public CommonResult<?> getSignById(@RequestParam("id") Integer id){
        return CommonResult.success(userSignService.getUserSignById(id));
    }

    @PostMapping("/addSign")
    public CommonResult<?> addSign(@RequestBody UserSign userSign){
        SysUser sysUser = getSysUser();
        userSign.setUid(Math.toIntExact(sysUser.getUserId()));
        return CommonResult.success(userSignService.addUserSign(userSign));
    }
    @GetMapping("/getDeptSignToday")
    @RequiresRoles(value = {"admin","boss","hr"},logical = Logical.OR)
    public CommonResult<?> getDeptSignToday(){
        SysUser sysUser = getSysUser();
        return CommonResult.success(userSignService.getUserSignToday(Math.toIntExact(sysUser.getDeptId())));
    }

    @PostMapping("/exportSign")
    @RequiresRoles(value = {"admin","boss","hr"},logical = Logical.OR)
    public CommonResult<?> exportSign(@RequestParam("begin") String begin,@RequestParam("end") String end){
        try {
            List<UserSignVo> allUserSignsByTime = userSignService.getAllUserSignsByTime(begin, end);
            ExcelUtil<UserSignVo> util = new ExcelUtil<>(UserSignVo.class);
            return CommonResult.success(util.exportExcel(allUserSignsByTime, "【 工作计划】数据"));
        }catch (Exception e){
            return CommonResult.erro(e.getMessage());
        }

    }
}
